﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GFMS.APP.App_UI.Commons;

using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;
using Telerik.Web.UI;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class MoneyRequestSalaries : System.Web.UI.UserControl
    {
        Main main = new Main();
        IBLL_FIM_MoneyRequests mr = new B_FIM_MoneyRequests();
        IBLL_FIM_MoneyRequestDtls mrd = new B_FIM_MoneyRequestDtls();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        IBLL_HRM_PR_PayrollDtls prd = new B_HRM_PR_PayrollDtls();
        IBLL_HRM_PS_EmployeeTypes et = new B_HRM_PS_EmployeeTypes();
        IBLL_HRM_PR_ExtraTypes ext = new B_HRM_PR_ExtraTypes();
        IBLL_BGM_ProjectOperationExpense opexp = new B_BGM_ProjectOperationExpense();

        protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList_BgmProject1.Child = DropDownList_BgmProjectExpense1.GetComboBox();

            if (!IsPostBack)
            {
                LoadDDLEmpTypeSeq();
            }
        }

        public void LoadMoneyRequestSalaries(string strMoneyReqSeq, string strBgmSourceSeq, string strBgmYear)
        {
            string strBgmMonth;

            lblMoneyReqSeq.Text = strMoneyReqSeq;
            lblBgmSourceSeq.Text = strBgmSourceSeq;
            lblBgmYear.Text = strBgmYear;
            DropDownList_BgmProject1.LoadControl(strBgmSourceSeq, strBgmYear, null, null, null);

            //LoadControl(strBgmSourceSeq, strBgmYear, null, null, null);

            if (strMoneyReqSeq == null || strMoneyReqSeq.Length == 0)
            {
                lblWorkAreaSeq.Text = Session["UserWorkAreaSeq"].ToString();
                dpMoneyReqDate.SetValue(DateTime.Today);
                txtPayYear.Text = (DateTime.Today.Year + 543).ToString();
                ddlPayMonth.SelectedValue = DateTime.Today.Month.ToString();
                DropDownList_BgmProjectExpense1.LoadControl("-1", lblBgmSourceSeq.Text, null, null);
                strBgmMonth = main.GetBgmMonth(strBgmSourceSeq, DateTime.Today).ToString();
                GetPaySeq();
                LoadDDLExtSeq();

                if (ddlExtSeq.Items.Count > 0)
                {
                    ddlExtSeq.SelectedIndex = 0;
                }

                LoadDataRadGrid1(lblPaySeq.Text, ddlExtSeq.SelectedValue);

                ddlEmpTypeSeq.Enabled = true;
                ddlPayMonth.Enabled = true;
                txtPayYear.Enabled = true;
            }
            else
            {
                M_FIM_MoneyRequests reqInfo = mr.GetInfo(strMoneyReqSeq);
                IList<M_FIM_MoneyRequestDtls> reqdtlsInfo = mrd.GetFull(strMoneyReqSeq);

                dpMoneyReqDate.SetValue(reqInfo.money_req_date);
                lblRefNo.Text = reqInfo.ref_no;
                lblWorkAreaSeq.Text = reqInfo.work_area_seq.ToString();
                DropDownList_BgmProject1.SetSelectedValue(reqInfo.bgm_proj_oper_seq.ToString());
                DropDownList_BgmProject1.ddlBgmProjectSeq_SelectedIndexChanged(null, null);
                DropDownList_BgmProjectExpense1.SetSelectedValue(reqdtlsInfo[0].bgm_proj_oper_exp_seq.ToString());

                IList<M_HRM_PR_PayrollDtls> payrolldtlsInfo = prd.GetFullFromMoneyReqSeq(strMoneyReqSeq);

                ddlEmpTypeSeq.SelectedValue = payrolldtlsInfo[0].emp_type_seq.ToString();
                txtPayYear.Text = payrolldtlsInfo[0].pay_year.ToString();
                ddlPayMonth.SelectedValue = payrolldtlsInfo[0].pay_month.ToString();

                LoadDataRadGrid1(payrolldtlsInfo[0].pay_seq.ToString(), payrolldtlsInfo[0].ext_seq.ToString());

                strBgmMonth = main.GetBgmMonth(strBgmSourceSeq, reqInfo.money_req_date).ToString();

                lblPaySeq.Text = payrolldtlsInfo[0].pay_seq.ToString();
                ddlEmpTypeSeq.Enabled = false;
                ddlPayMonth.Enabled = false;
                txtPayYear.Enabled = false;

                LoadDDLExtSeq();
                ddlExtSeq.SelectedValue = payrolldtlsInfo[0].ext_seq.ToString();
            }
        }

        protected void LoadDataRadGrid1()
        {
            string strPaySeq = lblPaySeq.Text;
            string strExtSeq = ddlExtSeq.SelectedValue;

            strPaySeq = (strPaySeq.Length > 0 ? strPaySeq : "-1");
            strExtSeq = (strExtSeq.Length > 0 ? strExtSeq : "-1");

            RadGrid1.DataSource = prd.GetSummaryByJobsiteGFMIS(strPaySeq, strExtSeq, null);
            RadGrid1.DataBind();
        }

        protected void LoadDataRadGrid1(string strPaySeq, string strExtSeq)
        {
            strPaySeq = (strPaySeq.Length > 0 ? strPaySeq : "-1");
            strExtSeq = (strExtSeq.Length > 0 ? strExtSeq : "-1");

            RadGrid1.DataSource = prd.GetSummaryByJobsiteGFMIS(strPaySeq, strExtSeq, null);
            RadGrid1.DataBind();
        }

        protected void LoadDDLEmpTypeSeq()
        {
            ddlEmpTypeSeq.DataSource = et.GetList();
            ddlEmpTypeSeq.DataBind();

            ddlEmpTypeSeq.SelectedIndex = 0;
            LoadDDLExtSeq();
        }

        protected void LoadDDLExtSeq()
        {
            string strPaySeq = lblPaySeq.Text;
            strPaySeq = (strPaySeq.Length > 0 ? strPaySeq : "-1");

            ddlExtSeq.DataSource = ext.GetMoneyReqIncomeList(strPaySeq);
            ddlExtSeq.DataBind();

            ddlExtSeq.SelectedIndex = 0;
        }

        protected string GetPaySeq()
        {
            string strEmpTypeSeq = ddlEmpTypeSeq.SelectedValue;
            string strPayYear = txtPayYear.Text;
            string strPayMonth = ddlPayMonth.SelectedValue;
            string strPaySeq = prd.GetPaySeq(strEmpTypeSeq, strPayYear, strPayMonth);

            lblPaySeq.Text = strPaySeq;

            return strPaySeq;
        }

        protected void SumAmount()
        {
            double dSumAmount = 0;
            double dAmount = 0;

            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                if (RadGrid1.Items[i].Selected)
                {
                    dAmount = main.ConvertNullDouble(RadGrid1.Items[i]["amount_pay"].Text).Value;
                    dSumAmount += dAmount;
                }
            }

            lblSumAmount.Text = dSumAmount.ToString("#,###.##");
        }

        protected bool CheckData()
        {
            bool bOK = true;
            string strMoneyReqDate = dpMoneyReqDate.GetStringValue();
            string strEmpTypeSeq = ddlEmpTypeSeq.SelectedValue;
            string strYear = txtPayYear.Text;
            string strMonth = ddlPayMonth.SelectedValue;
            string strExtSeq = ddlExtSeq.SelectedValue;
            string strBgmProject = DropDownList_BgmProject1.GetSelectedValue();
            string strBgmProjectExpense = DropDownList_BgmProjectExpense1.GetSelectedValue();
            double dSumAmount = Convert.ToDouble(lblSumAmount.Text); 

            if (strMoneyReqDate.Length == 0)
            {
                imgMoneyReqDate.Visible = true;
                bOK = false;
            }
            else if (strEmpTypeSeq.Length == 0)
            {
                imgEmpTypeSeq.Visible = true;
                bOK = false;
            }
            else if (strYear.Length == 0)
            {
                imgPayYear.Visible = true;
                bOK = false;
            }
            else if (strMonth.Length == 0)
            {
                imgPayMonth.Visible = true;
                bOK = false;
            }
            else if (strExtSeq.Length == 0)
            {
                imgExtSeq.Visible = true;
                bOK = false;
            }
            else if (strBgmProject.Length == 0)
            {
                imgBgmProject.Visible = true;
                bOK = false;
            }
            else if (strBgmProjectExpense.Length == 0)
            {
                imgBgmProjectExpense.Visible = true;
                bOK = false;
            }
            else if (dSumAmount == 0)
            {
                bOK = false;
            }
            else
            {
                imgMoneyReqDate.Visible = false;
                imgEmpTypeSeq.Visible = false;
                imgPayYear.Visible = false;
                imgPayMonth.Visible = false;
                imgExtSeq.Visible = false;
                imgBgmProject.Visible = false;
                imgBgmProjectExpense.Visible = false;
            }

            return bOK;
        }

        protected void ClearControl1()
        {

        }

        //=====================================================================

        public bool Insert()
        {
            bool bOK = true;
            M_FIM_MoneyRequests info = new M_FIM_MoneyRequests();
            M_FIM_SystemControls sys = main.GetSystemControls();
            string strMoneyReqSeq;
            string strBgmSourceSeq = lblBgmSourceSeq.Text;
            string strBgmYear = lblBgmYear.Text;
            string strWorkAreaSeq = lblWorkAreaSeq.Text;
            string strBgmProjOperExpSeq = DropDownList_BgmProjectExpense1.GetSelectedValue();
            string strEventSeq = "14";
            string strUseAdvance = "0";
            string strMoneyReqDate = dpMoneyReqDate.GetStringValue();
            string strMoneyReqName = "รอเบิก" + ddlExtSeq.Text + "ของ" + ddlEmpTypeSeq.Text + " ประจำเดือน "
                + ddlPayMonth.Text + " " + txtPayYear.Text;
            string strRefNo = lblRefNo.Text;

            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            if (!CheckData())
            {
                bOK = false;
                return bOK;
            }

            string strBgmExpGrpSeq = opexp.GetInfo(strBgmProjOperExpSeq).m_bgm_projectexpense.m_bgm_mas_expense.bgm_exp_grp_seq.ToString();

            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.bgm_year = main.ConvertNullInt(strBgmYear);
            info.work_area_seq = main.ConvertNullInt(strWorkAreaSeq);
            info.bgm_exp_grp_seq = main.ConvertNullInt(strBgmExpGrpSeq);
            info.event_seq = main.ConvertNullInt(strEventSeq);
            info.use_advance = strUseAdvance;
            info.money_req_date = main.ConvertNullDateTime(strMoneyReqDate);
            info.money_req_name = strMoneyReqName;
            info.ref_no = strRefNo;
            info.approver_seq = null;
            info.emp_seq = main.ConvertNullInt(strUserID);
            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = main.ConvertNullDateTime(dtNow);

            strMoneyReqSeq = mr.Insert(info);

            string strSalaryExtSeq = null;
            string strTaxExtSeq = null;
            string strFundExtSeq = null;
            string strEmpTypeSeq = ddlEmpTypeSeq.SelectedValue;

            switch (strEmpTypeSeq)
            {
                case "1" :
                    strSalaryExtSeq = SystemInfo._SalaryExtSeq1;
                    strTaxExtSeq = SystemInfo._TaxExtSeq1;
                    strFundExtSeq = SystemInfo._FundExtSeq1;
                    break;
                case "2" :
                    strSalaryExtSeq = SystemInfo._SalaryExtSeq2;
                    strTaxExtSeq = SystemInfo._TaxExtSeq2;
                    strFundExtSeq = SystemInfo._FundExtSeq2;
                    break;
                case "3" :
                    strSalaryExtSeq = SystemInfo._SalaryExtSeq3;
                    strTaxExtSeq = SystemInfo._TaxExtSeq3;
                    strFundExtSeq = SystemInfo._FundExtSeq3;
                    break;
            }

            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                if (RadGrid1.Items[i].Selected)
                {
                    M_FIM_MoneyRequestDtls dtlsInfo = new M_FIM_MoneyRequestDtls();
                    string strJobSiteSeq = main.WebString(RadGrid1.Items[i]["job_site_seq"].Text);
                    string strJobSiteGFMIS = main.WebString(RadGrid1.Items[i]["job_site_gfmis"].Text);
                    string strAmount = main.WebString(RadGrid1.Items[i]["amount_pay"].Text);
                    Nullable<double> dAmountTax = 0;
                    Nullable<double> dAmountFund = 0;

                    if (strSalaryExtSeq == ddlExtSeq.SelectedValue)
                    {
                        dAmountTax = prd.GetSummaryByJobsiteGFMIS(lblPaySeq.Text, strTaxExtSeq, strJobSiteGFMIS)[0].amount_pay;
                        dAmountFund = prd.GetSummaryByJobsiteGFMIS(lblPaySeq.Text, strFundExtSeq, strJobSiteGFMIS)[0].amount_pay;
                    }

                    //dtlsInfo.money_req_dtls_seq = null;
                    dtlsInfo.money_req_seq = main.ConvertNullInt(strMoneyReqSeq);
                    dtlsInfo.money_req_dtls_name = null;
                    dtlsInfo.bgm_proj_oper_exp_seq = main.ConvertNullInt(strBgmProjOperExpSeq);
                    dtlsInfo.job_site_seq = main.ConvertNullInt(strJobSiteSeq);
                    dtlsInfo.percent_vat = 0;
                    dtlsInfo.percent_tax = 0;
                    dtlsInfo.amount = main.ConvertNullDouble(strAmount);
                    dtlsInfo.amount_vat = 0;
                    dtlsInfo.amount_tax = dAmountTax;
                    dtlsInfo.amount_fund = dAmountFund;
                    //dtlsInfo.currency_seq = sys.default_currency_seq;
                    //dtlsInfo.exchange_rate = 1;
                    //dtlsInfo.bath_amount = main.ConvertNullDouble(strAmount);
                    //dtlsInfo.bath_amount_vat = 0;
                    //dtlsInfo.bath_amount_tax = dAmountTax;
                    //dtlsInfo.bath_amount_fund = dAmountFund;
                    dtlsInfo.created_by = main.ConvertNullInt(strUserID);
                    dtlsInfo.created_when = main.ConvertNullDateTime(dtNow);

                    string strMoneyReqDtlsSeq = mrd.Insert(dtlsInfo);
                    string strPaySeq = main.WebString(lblPaySeq.Text);

                    prd.UpdateMoneyReqDtlsSeq(strPaySeq, strJobSiteSeq, strMoneyReqDtlsSeq, strUserID);
                }
            }

            ClearControl1();
            return bOK;
        }

        public bool Update()
        {
            bool bOK = true;
            M_FIM_MoneyRequests info = new M_FIM_MoneyRequests();
            M_FIM_SystemControls sys = main.GetSystemControls();
            string strMoneyReqSeq = lblMoneyReqSeq.Text;
            string strBgmSourceSeq = lblBgmSourceSeq.Text;
            string strBgmYear = lblBgmYear.Text;
            string strWorkAreaSeq = lblWorkAreaSeq.Text;
            string strBgmProjOperExpSeq = DropDownList_BgmProjectExpense1.GetSelectedValue();
            string strEventSeq = "14";
            string strUseAdvance = "0";
            string strMoneyReqDate = dpMoneyReqDate.GetStringValue();
            string strMoneyReqName = "รอเบิก" + ddlExtSeq.Text + "ของ" + ddlEmpTypeSeq.Text + " ประจำเดือน "
                + ddlPayMonth.Text + " " + txtPayYear.Text;
            string strRefNo = lblRefNo.Text;

            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            if (!CheckData())
            {
                bOK = false;
                return bOK;
            }

            string strBgmExpGrpSeq = opexp.GetInfo(strBgmProjOperExpSeq).m_bgm_projectexpense.m_bgm_mas_expense.bgm_exp_grp_seq.ToString();

            info.money_req_seq = Convert.ToInt32(strMoneyReqSeq);
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.bgm_year = main.ConvertNullInt(strBgmYear);
            info.work_area_seq = main.ConvertNullInt(strWorkAreaSeq);
            info.bgm_exp_grp_seq = main.ConvertNullInt(strBgmExpGrpSeq);
            info.event_seq = main.ConvertNullInt(strEventSeq);
            info.use_advance = strUseAdvance;
            info.money_req_date = main.ConvertNullDateTime(strMoneyReqDate);
            info.money_req_name = strMoneyReqName;
            info.ref_no = strRefNo;
            info.approver_seq = null;
            info.emp_seq = main.ConvertNullInt(strUserID);
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);

            mr.Update(info);

            string strSalaryExtSeq = null;
            string strTaxExtSeq = null;
            string strFundExtSeq = null;
            string strEmpTypeSeq = ddlEmpTypeSeq.SelectedValue;

            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                GridDataItem item = RadGrid1.Items[i];
                string strMoneyReqDtlsSeq = main.WebString(item["money_req_dtls_seq"].Text);
                string strJobSiteSeq = main.WebString(item["job_site_seq"].Text);
                string strJobSiteGFMIS = main.WebString(item["job_site_gfmis"].Text);
                M_FIM_MoneyRequestDtls dtlsInfo = new M_FIM_MoneyRequestDtls();

                if (item.Selected)
                {
                    string strAmount = main.WebString(item["amount_pay"].Text);
                    Nullable<double> dAmountTax = 0;
                    Nullable<double> dAmountFund = 0;

                    if (strSalaryExtSeq == ddlExtSeq.SelectedValue)
                    {
                        dAmountTax = prd.GetSummaryByJobsiteGFMIS(lblPaySeq.Text, strTaxExtSeq, strJobSiteGFMIS)[0].amount_pay;
                        dAmountFund = prd.GetSummaryByJobsiteGFMIS(lblPaySeq.Text, strFundExtSeq, strJobSiteGFMIS)[0].amount_pay;
                    }

                    //dtlsInfo.money_req_dtls_seq = null;
                    dtlsInfo.money_req_seq = main.ConvertNullInt(strMoneyReqSeq);
                    dtlsInfo.money_req_dtls_name = null;
                    dtlsInfo.bgm_proj_oper_exp_seq = main.ConvertNullInt(strBgmProjOperExpSeq);
                    dtlsInfo.percent_vat = 0;
                    dtlsInfo.percent_tax = 0;
                    dtlsInfo.amount = main.ConvertNullDouble(strAmount);
                    dtlsInfo.amount_vat = 0;
                    dtlsInfo.amount_tax = dAmountTax;
                    dtlsInfo.amount_fund = dAmountFund;
                    //dtlsInfo.currency_seq = sys.default_currency_seq;
                    //dtlsInfo.exchange_rate = 1;
                    //dtlsInfo.bath_amount = main.ConvertNullDouble(strAmount);
                    //dtlsInfo.bath_amount_vat = 0;
                    //dtlsInfo.bath_amount_tax = dAmountTax;
                    //dtlsInfo.bath_amount_fund = dAmountFund;

                    if (strMoneyReqDtlsSeq.Length == 0)
                    {
                        dtlsInfo.created_by = main.ConvertNullInt(strUserID);
                        dtlsInfo.created_when = main.ConvertNullDateTime(dtNow);

                        strMoneyReqDtlsSeq = mrd.Insert(dtlsInfo);
                    }
                    else
                    {
                        dtlsInfo.money_req_dtls_seq = main.ConvertNullInt(strMoneyReqDtlsSeq);
                        dtlsInfo.updated_by = main.ConvertNullInt(strUserID);
                        dtlsInfo.updated_when = main.ConvertNullDateTime(dtNow);

                        mrd.Update(dtlsInfo);
                    }

                    string strPaySeq = main.WebString(lblPaySeq.Text);
                    prd.UpdateMoneyReqDtlsSeq(strPaySeq, strJobSiteSeq, strMoneyReqDtlsSeq, strUserID);
                }
                else
                {
                    if (strMoneyReqDtlsSeq.Length > 0)
                    {
                        dtlsInfo.money_req_dtls_seq = main.ConvertNullInt(strMoneyReqDtlsSeq);
                        dtlsInfo.updated_by = main.ConvertNullInt(strUserID);
                        dtlsInfo.updated_when = main.ConvertNullDateTime(dtNow);

                        mrd.Delete(dtlsInfo);

                        string strPaySeq = main.WebString(lblPaySeq.Text);
                        prd.UpdateMoneyReqDtlsSeq(strPaySeq, strJobSiteSeq, strMoneyReqDtlsSeq, strUserID);
                    }
                }
            }

            ClearControl1();
            return bOK;
        }

        protected void ddlEmpTypeSeq_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            GetPaySeq();
            LoadDDLExtSeq();
            LoadDataRadGrid1();
        }

        protected void ddlExtSeq_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDataRadGrid1();
        }

        protected void txtPayYear_TextChanged(object sender, EventArgs e)
        {
            GetPaySeq();
            LoadDDLExtSeq();
            LoadDataRadGrid1();
        }

        protected void ddlPayMonth_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            GetPaySeq();
            LoadDDLExtSeq();
            LoadDataRadGrid1();
        }

        protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SumAmount();
        }
    }
}